﻿using System;
using System.Threading;
using System.Globalization;

// 定义闹钟类
class AlarmClock
{
    // 定义嘀嗒事件
    public event EventHandler Tick;
    // 定义响铃事件
    public event EventHandler Alarm;
    private DateTime alarmTime = DateTime.MinValue;
    private bool alarmRinging = false;
    public void Run()
    {
        Console.WriteLine("按空格键停止显示时间...");
        Console.WriteLine("按R设置闹铃时间...");
        Console.WriteLine();
        while (true)
        {
            DateTime now = DateTime.Now;
            Console.SetCursorPosition(0, 3);
            Console.Write(now.ToString("yyyy-MM-dd HH:mm:ss"));
            // 触发嘀嗒事件
            OnTick();
            if (Console.KeyAvailable)
            {
                ConsoleKeyInfo keyInfo = Console.ReadKey(true);
                if (keyInfo.Key == ConsoleKey.R)
                {
                    Console.WriteLine("\n请输入响铃时间（ 例如 12:30:45）：");
                    while (true)
                    {
                        string input = Console.ReadLine();
                        string format = "HH:mm:ss";
                        if (DateTime.TryParseExact(input, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime time))
                        {
                            DateTime today = DateTime.Today;
                            alarmTime = today.AddHours(time.Hour).AddMinutes(time.Minute).AddSeconds(time.Second);
                            if (alarmTime < now)
                            {
                                alarmTime = alarmTime.AddDays(1);
                            }
                            Console.WriteLine("输入的时间是：" + alarmTime.ToString("HH:mm:ss"));
                            TimeSpan difference = alarmTime - now;
                            Console.WriteLine("距离响铃时间：" + difference.ToString());
                            break;
                        }
                        else
                        {
                            Console.WriteLine("输入格式不正确，请按照时：分：秒的格式重新输入：");
                        }
                    }
                }
                else if (keyInfo.Key == ConsoleKey.Spacebar)
                {
                    break;
                }
            }

            if (alarmTime != DateTime.MinValue && now >= alarmTime &&!alarmRinging)
            {
                // 触发响铃事件
                OnAlarm();
                alarmRinging = true;
                Console.WriteLine("\n闹铃响啦！");
                for (int i = 0; i < 5; i++)
                {
                    Console.Beep();
                    Thread.Sleep(1000);
                }
                alarmRinging = false;
                alarmTime = DateTime.MinValue;
            }

            Thread.Sleep(1000);
        }

        Console.WriteLine("\n时间显示已停止。");
    }

    protected virtual void OnTick()
    {
        Tick?.Invoke(this, EventArgs.Empty);
    }

    protected virtual void OnAlarm()
    {
        Alarm?.Invoke(this, EventArgs.Empty);
    }
}

class Program
{
    static void Main()
    {
        AlarmClock clock = new AlarmClock();
        clock.Tick += (sender, e) => Console.WriteLine("闹钟嘀嗒...");
        clock.Alarm += (sender, e) => Console.WriteLine("闹钟开始响铃！");
        clock.Run();
    }
}